﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using FPC.Offer.Common;
using System.Web.SessionState;
namespace FPC.Offer.UI.GeneralProcessor.Message
{
    /// <summary>
    /// ReceiveMessage 的摘要说明
    /// </summary>
    public class ReceiveMessage : IHttpHandler,IReadOnlySessionState
    {
        FPC.Offer.Service.Message_ReceiveMessageService PoliceMessage = new Service.Message_ReceiveMessageService();
        FPC.Offer.Service.Message_AddMessageService SendMessage = new Service.Message_AddMessageService();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain"; 
            var Actioon = context.Request["Actioon"].ToString();//枚举判断进入哪个方法
            var name = context.Request["name"];
         
           var ToUserName = context.Session[FPC.Offer.Common.SystemKeys.COOKIE_USER_NAME_REMEMBER].ToString();
                if (Actioon == EnumAction.Select.ToString())
                {
                    if (name != null)
                    {
                        var list = (
                              from n in PoliceMessage.Select()
                              join w in SendMessage.Select()
                              on n.MessageID equals w.MessageId
                              select new
                              {
                                  ReceiveID = n.ReceiveID,
                                  MessageState = n.MessageState,
                                  FromUser = w.FromUser,
                                  MessageTitle = w.MessageTitle,
                                  MessageContent = w.MessageContent,
                                  MessageDate = w.MessageDate,
                                  ToUser = w.ToUser,
                                  SendState=w.SendState,
                              }).ToList();
                        var ShowLisst = list.Where(p => p.ToUser == ToUserName && p.MessageTitle == name).ToList();
                        ShowLisst = ShowLisst.Where(p => p.MessageState.Replace(" ", "") != "已删除").ToList();
                        context.Response.Write(JsonConvert.SerializeObject(ShowLisst));

                    }
                    else
                    {
                        var list = (
                                 from n in PoliceMessage.Select()
                                 join w in SendMessage.Select()
                                 on n.MessageID equals w.MessageId
                                 select new
                                 {
                                     ReceiveID = n.ReceiveID,
                                     MessageState = n.MessageState,
                                     FromUser = w.FromUser,
                                     MessageTitle = w.MessageTitle,
                                     MessageContent = w.MessageContent,
                                     MessageDate = w.MessageDate,
                                     ToUser = w.ToUser,
                                     SendState = w.SendState,
                                 }).ToList();
                        var ShowLisst = list.Where(p => p.ToUser == ToUserName).ToList();
                        ShowLisst = ShowLisst.Where(p => p.MessageState.Replace(" ", "") != "已删除").ToList();
                        context.Response.Write(JsonConvert.SerializeObject(ShowLisst));
                    
                    }
                }
             //显示未读信息条数
            if (Actioon == EnumAction.Update.ToString())
            {
                var list = (
                                 from n in PoliceMessage.Select()
                                 join w in SendMessage.Select()
                                 on n.MessageID equals w.MessageId
                                 select new
                                 {
                                     ReceiveID = n.ReceiveID,
                                     MessageState = n.MessageState,
                                     FromUser = w.FromUser,
                                     MessageTitle = w.MessageTitle,
                                     MessageContent = w.MessageContent,
                                     MessageDate = w.MessageDate,
                                     ToUser = w.ToUser,
                                     SendState = w.SendState,
                                 }).ToList();
                var lis = list.Where(p => p.ToUser == ToUserName).ToList();
                int i = lis.Where(p => p.MessageState == "未读").Count();
                context.Response.Write(i);
            }
            if (Actioon == EnumAction.Delete.ToString())
            {
                context.Response.Write(JsonConvert.SerializeObject(PoliceMessage.Select()));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}